package
{
	import flashx.textLayout.tlf_internal;
	
	import org.flixel.*;
	import org.flixel.system.FlxAnim;
	
	public class Obstacle extends FlxSprite {
		
	   private const FADING:Number = 0.01;
	   public static const OBSTACLE_NORMAL:int = 0;
	   public static const OBSTACLE_SHOWING:int = 1;
	   public static const OBSTACLE_CHANGING:int = 2;
	   public static const OBSTACLE_OVERLAPPED:int = 3;
	   public static const OBSTACLE_CHANGING_BACK:int = 4;
	   public static const OBSTACLE_DESTROYED:int = 5;
	   protected var theGraphic:FlxSprite;
	   
	   //list of normal animations, must have something 
	   //for normal, passing, changing, changingBack. (if the obstacle is destroyed just use the first three)
	   private var imgList:Array = new Array(Res.iceObsAnim,Res.fireWallAnim,Res.mudObsAnim,Res.steamObsAnim,Res.lightningObsAnim,Res.sandObsAnim,Res.woodObsAnim,Res.metalObsAnim,Res.glassObsAnim,Res.rainbowObsAnim);
	   
	   protected var obstacleState:int;
	   protected var animationState:int;
	   protected var waitForAnimationChange:Boolean;
	   protected var timer:Number;
	   
	   protected var destroyed:Boolean;
	   /* known obstacles:
	   Lake (Pass through as ice) (5 in tut)	 -  50
	   Fire Wall (Pass through as water) (8 in tut)  - 51
	   Mud Pit (Pass through as mud water-earth) (3 in tut) - 52
	   Steam Vents (Pass through as steam) (2 in tut) -53
	   Lightning Trap (Pass through as lightning) (1 in tut) -54
	   Sand Storm (Pass through as sand) - 55
	   */
	   private const mapType:Array = new Array("iceField", "fireWall", "mudPit", "steamGeyser", "lightningTrap", "sandStorm", "abyss", "crusher", "laserOfDeath", "rainbow");
	   private const passAbleType:Array = new Array(true,true,true,true,true,true,false,false,false,false);
	   private const typeList:Array = new Array(Elemental.ICE, Elemental.FIRE,Elemental.MUD,Elemental.STEAM,Elemental.LIGHTNING,Elemental.SAND,Elemental.WOOD,Elemental.METAL, Elemental.GLASS, Elemental.RAINBOW);
	   private var type:int;
		

	   private var played:Boolean;
	   
		public function Obstacle(X:int, Y:int, type:int, obsGraphic:FlxSprite)
		{
			super(X - 3, Y - 3, null);
			this.type = type;
			this.immovable = true;
			
			theGraphic = obsGraphic
			
			//loadGraphic(imgList[type], true, false, 46, 46, false);
//			addAnimation("normal", [0,1,2,3,4,5,6,7], 8);
//			addAnimation("showing", [8,9,10,11,12,13,14,15], 8);
//			addAnimation("passing", [16,17,18,19,20,21,22,23], 8);
//			addAnimation("changing", [24,25,26,27,28,29,30,31], 16);
//			addAnimation("changingBack", [32,33,34,35,36,37,38,39], 16);
//			addAnimationCallback(animationListener);
//			play("normal");
			
			obstacleState = OBSTACLE_NORMAL;
			animationState = OBSTACLE_NORMAL;
			timer = 0;
			waitForAnimationChange = false;
			destroyed = false;
			
			this.loadGraphic(Res.obsBoundingBox,false,false,46,46,false);
			theGraphic.loadGraphic(imgList[type],true,false,46,46,false);
			theGraphic.addAnimation("normal", [0,1,2,3,4,5,6,7], 8);
			theGraphic.addAnimation("showing", [8,9,10,11,12,13,14,15], 8);
			theGraphic.addAnimation("passing", [16,17,18,19,20,21,22,23], 8);
			theGraphic.addAnimation("changing", [24,25,26,27,28,29,30,31], 16);
			theGraphic.addAnimation("changingBack", [32,33,34,35,36,37,38,39], 16);
			theGraphic.addAnimationCallback(animationListener);
			theGraphic.play("normal");
			
			played = false;
		}
		
		public override function update():void 
		{  
			super.update();
			if (waitForAnimationChange)
				timer += FlxG.elapsed;
		
		}
		
		
		public function typeMatch(player:Player):Boolean{
			if (!destroyed) {
				return player.element == this.typeList[type];
			} else {
				return true;
			}
		}
		
		public function isPassAble():Boolean{
			return this.passAbleType[type];
		}
		
		public function animationListener(name:String, frameNumber:int, frameIndex:int):void {
			if (obstacleState == OBSTACLE_NORMAL) {
				if (animationState != OBSTACLE_NORMAL) {
					animationState = OBSTACLE_NORMAL;
					frame = 0;
					theGraphic.play("normal");
				}
			} else if (obstacleState == OBSTACLE_DESTROYED) {
				if (animationState != OBSTACLE_DESTROYED) {
					animationState = OBSTACLE_DESTROYED;
					frame = 0;
					theGraphic.play("passing");
				}
			} else if (obstacleState == OBSTACLE_CHANGING) {
				if (animationState != OBSTACLE_CHANGING) {
					animationState = OBSTACLE_CHANGING;
					frame = 16;
					theGraphic.play("changing");
				}
				if (frameIndex == 30) {
					obstacleState = OBSTACLE_OVERLAPPED;
				}
			} else if (obstacleState == OBSTACLE_OVERLAPPED) {
				if (animationState != OBSTACLE_OVERLAPPED) {
					animationState = OBSTACLE_OVERLAPPED;
					frame = 8;
					theGraphic.play("passing")
					waitForAnimationChange = true;
					timer = 0;
				}
				if (timer > .5) {
					if (isPassAble()) {
						obstacleState = OBSTACLE_CHANGING_BACK;
					} else {
						obstacleState = OBSTACLE_DESTROYED;
						destroyed = true;
					}
					waitForAnimationChange = false;
					timer = 0;
				}
			} else if (obstacleState == OBSTACLE_CHANGING_BACK) {
				if (animationState != OBSTACLE_CHANGING_BACK) {
					animationState = OBSTACLE_CHANGING_BACK;
					frame = 24;
					theGraphic.play("changingBack");
				}
				if (frameIndex == 38) {
					obstacleState = OBSTACLE_NORMAL;
				}
			} else if (obstacleState == OBSTACLE_SHOWING) {
				if (animationState != OBSTACLE_SHOWING) {
					animationState = OBSTACLE_SHOWING;
					frame = 8;
					theGraphic.play("showing")
					waitForAnimationChange = true;
					timer = 0;
				}
				if (timer > 2) {
					obstacleState = OBSTACLE_NORMAL;
					waitForAnimationChange = false;
					timer = 0;
				}
			}
		}
		
		public function playElementsAnimation():void {
			if (obstacleState == OBSTACLE_NORMAL) {
				obstacleState = OBSTACLE_SHOWING;
			} else if (obstacleState == OBSTACLE_SHOWING) {
				timer = 0;
			}
		}
		
		//Plays passing animation for obstacle
		public function playPassableAnimation():void {
			if (!isPassAble() && !played && !Music.MUTE){
					FlxG.play(Res.boom);
					played=true;
			}
			if (obstacleState == OBSTACLE_NORMAL) {
				obstacleState = OBSTACLE_CHANGING;
			} else if (obstacleState == OBSTACLE_OVERLAPPED) {
				timer = 0;
			} else if (obstacleState == OBSTACLE_SHOWING) {
				waitForAnimationChange = false;
				timer = 0;
				obstacleState = OBSTACLE_CHANGING;
			}
		}
		
	}
}